package org.smart4j.framework.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author djw
 * @time 2017/10/25 9:14
 */
public class JsonUtil {
    private static final Logger LOGGER= LoggerFactory.getLogger(JsonUtil.class);
    private static final ObjectMapper OBJECT_MAPPER=new ObjectMapper();

    public static <T> String toJson(T obj) throws JsonProcessingException {
        String json;

        json=OBJECT_MAPPER.writeValueAsString(obj);

        return json;
    }

    public static <T> T fromJson(String json,Class<T> type) throws IOException {
        T pojo;

        pojo=OBJECT_MAPPER.readValue(json,type);

        return  pojo;
    }
}
